package view.ventanas_principales.panels;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import model.Instructor;
import model.comunication.IInstructor;
import view.instructor.VerInstructor;

public class InstructoresPanel extends JPanel {

	private static final long serialVersionUID = 1L;
	private DefaultTableModel modeloInstructores;
	private JTable tableInstructores;
	private IInstructor cmmInstructor;
	private List<Instructor> listaInstructores = null;

	public InstructoresPanel() {
		inicialize();
		loadTable();
	}

	public void inicialize() {
		tableInstructores = new JTable();
		tableInstructores.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "Nombre", "Apellido", "DNI" }) {

			private static final long serialVersionUID = 1L;

			@SuppressWarnings({ "unchecked", "rawtypes" })
			@Override
			public Class getColumnClass(int columna) {
				if (columna == 3) {
					return Calendar.class;

				}
				return String.class;
			}
		});
		setLayout(new BorderLayout(0, 0));
		tableInstructores.getTableHeader().setReorderingAllowed(false);
		this.add(tableInstructores);
		JScrollPane scrollEvento = new JScrollPane(tableInstructores);
		this.add(scrollEvento);

		modeloInstructores = (DefaultTableModel) tableInstructores.getModel();

		JPanel buttonPanel = new JPanel();
		FlowLayout fl_buttonPanel = (FlowLayout) buttonPanel.getLayout();
		fl_buttonPanel.setAlignment(FlowLayout.LEFT);
		add(buttonPanel, BorderLayout.SOUTH);

		JButton btnVer = new JButton("Ver");
		btnVer.setIcon(new ImageIcon(EventosPanel.class.getResource("/Icon_/search_16.png")));
		btnVer.setFont(new Font("Tahoma", Font.BOLD, 11));
		buttonPanel.add(btnVer);

		btnVer.addActionListener(new ActionListener() {
			@SuppressWarnings("unused")
			public void actionPerformed(ActionEvent arg0) {
				int selectedRow = tableInstructores.getSelectedRow();
				if (!(selectedRow < 0)) {
					String value = (String) tableInstructores.getValueAt(tableInstructores.getSelectedRow(), 0);
					VerInstructor verInstructor = new VerInstructor(listaInstructores.get(selectedRow));
					verInstructor.setVisible(true);
				}
			}
		});

		JButton btnRegistrarHorario = new JButton("Registrar Horario");
		btnRegistrarHorario.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			}
		});
		buttonPanel.add(btnRegistrarHorario);
	}

	private void loadTable() {
		cmmInstructor = IInstructor.getInstance();
		listaInstructores = cmmInstructor.getAllInstructor();
		for (int i = 0; i < listaInstructores.size(); i++) {
			modeloInstructores.addRow(new Object[] { listaInstructores.get(i).getNombre(), listaInstructores.get(i).getApellido(), listaInstructores.get(i).getDNI() });
		}
	}
}
